package com.psfd.demo.controller;

import com.psfd.demo.bean.User;
import com.psfd.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/userinsert")
    public ModelAndView insert(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("useradd");
        return modelAndView;
    }


    @RequestMapping("/insertUser")
    public ModelAndView add(User user){
        System.err.println(user+"------------------");
        userService.save(user);
        return queryAll();
    }




    @RequestMapping("/userupdate")
    public ModelAndView userupdate(Integer userid){
        System.out.println(userid);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userid",userid);
        modelAndView.setViewName("userupdate");
        return modelAndView;
    }

    @RequestMapping("/updateUser")
    public ModelAndView updateUser(User user){
        System.err.println(user+"--------------------");
        userService.updateById(user);
        return queryAll();
    }

    @RequestMapping("/deleteUser")
    public ModelAndView delete(Integer userid){
        System.err.println(userid+"....................");
        userService.removeById(userid);
        return queryAll();
    }


    @RequestMapping("/userQueryAll")
    public ModelAndView queryAll(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userlist",userService.list());
        modelAndView.setViewName("userList");
        return modelAndView;
    }


//    @RequestMapping("/userreturnpage")
//    public ModelAndView returnpage(){
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("homepage");
//        return modelAndView;
//    }
}
